<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Core_Lib_Page.php");
require_once("../class/Common_Function.php");

//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 || $userRole == 8){
	//管理员+团队成员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}


//获取form值
$doFlag = $_POST["doFlag"];
$orderId = $_POST["orderId"];
if($doFlag == "doDelete"){
	$sql = "update orders set deliver_flag=3,tracking_number='',express_company=''  where id=".$orderId;
	$result = $db->Execute($sql);
}else if( $doFlag == "addTrackingNumber"){
	$orderId = $_POST["orderId"];
	$expressCompany = $_POST["express_company_".$orderId];
	$trackingNumber = $_POST["tracking_number_".$orderId];	
	$sql = "update orders set deliver_flag=8,trackingnumber_time=now(), tracking_number = '".$trackingNumber."',express_company='".$expressCompany."' where id=".$orderId;
	$db->Execute($sql);
}



$currentDeliverFlag = $_POST["currentDeliverFlag"];
//分页设置
$list_rows = 50; //每页显示的记录数
if($doFlag == "doSearch"){
	$searchValue = $_POST["search-value"];
	$sql_list = "SELECT COUNT(*) as total_rows  from orders o where (deliver_flag = 7 or deliver_flag = 8 or deliver_flag = 9) and ( order_no = '".$searchValue."' or tracking_number = '".$searchValue."')"; 
} else if($doFlag == "toDeliverFlag" && $currentDeliverFlag == 1){ //未发货的记录
	$sql_list = "SELECT COUNT(*) as total_rows  from orders o where (deliver_flag = 7)"; 
}else{
	$sql_list = "SELECT COUNT(*) as total_rows  from orders o where (deliver_flag = 7 or deliver_flag = 8 or deliver_flag = 9)"; 
}
$result = $db->Execute($sql_list);
if (!empty($result)) {
	$totalRows = $result->fields['total_rows'];
}
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => !empty($_POST['nowPage']) ? $_POST['nowPage'] : 1 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;


//显示列表
if($doFlag == "doSearch"){
	$searchValue = $_POST["search-value"];
	$sql = "SELECT * from orders o where (deliver_flag = 7 or deliver_flag = 8 or deliver_flag = 9) and ( order_no = '".$searchValue."' or tracking_number = '".$searchValue."')"; 
} else if($doFlag == "toDeliverFlag" && $currentDeliverFlag == 1){
	$sql = "SELECT * from orders o where deliver_flag = 7 order by deliver_time desc limit $limit_m,$limit_n"; //未发货的记录
}else{
	$sql = "SELECT * from orders o where (deliver_flag = 7 or deliver_flag = 8 or deliver_flag = 9) order by deliver_time desc limit $limit_m,$limit_n"; //default 
}
$result = $db->Execute($sql);


$express_company_10 = '';
$express_company_20 = '';
$express_company_30 = '';
$express_company_40 = '';
$express_company_50 = '';
$express_company_60 = '';
$express_company_70 = '';
$express_company_80 = '';
$express_company_90 = '';
//$express_company_100 = ''; //eub
$express_company_110 = '';
$express_company_1000 = '';


//统计当前没填快递单的记录总数
$sql_list_no_trackingnumber = "SELECT COUNT(*) as total_rows  from orders o where deliver_flag = 7"; 
$result_no_trackingnumber = $db->Execute($sql_list_no_trackingnumber);
if (!empty($result_no_trackingnumber)) {
	$total_no_trackingnumber = $result_no_trackingnumber->fields['total_rows'];
}else{
	$total_no_trackingnumber = 0;
}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "易运全球快递列表"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="listForm" action="../express/ezshippingList.php" method="post">
<div id="container" >	

<div id="search" class="leftDiv topDiv-10">
<input type="text" name="search-value" id="search-value" size="30" value="<?php echo $searchValue;?>">
<input type="button" name="Search" value="搜 索" onclick="doSearch()"> <font size="1">(网站ID/快递单号 完全匹配)</font>
</div>
<div class="clearBoth">&nbsp;</div>


<div class="grayTable">	
<?php
if($doFlag == "doSearch"){	
	echo "<b>搜索结果:</b>";
}else{
	echo "<b>易运全球 快递单列表:</b>";
}

$topTitle =  $topTitle."    <a href='javascript:toDeliverFlag(1)'>未发订单</a>";
$topTitle =  $topTitle."    <a href='javascript:toDeliverFlag(-1)'>全部订单</a>";
$topTitle =  $topTitle."    今天<font color=red>[".$total_no_trackingnumber."]</font>单";
$topTitle =  $topTitle."   <br><br><b>海关编码:</b> LED手电筒(8513101000)  激光模组(9013200090) 显微镜(90118000) <br><br>";
$topTitle =  $topTitle." <b>上海仓库地址:</b>  上海市闵行区中春路7333弄83号  Allen  电话：15000123869  15106603332 <br><br>";
echo $topTitle;
?>



<table >
<thead>
	<tr>
		<th scope="col" width=70>发货时间</th>
		<th scope="col" width=40>网站ID</th>
	    <th scope="col" >客户名字</th>		
		<th scope="col" >客户地址</th>	
		<th scope="col" >客户邮编</th>	
		<th scope="col" width=50>客户电话</th>	
		<th scope="col" width=30>备注</th>		
		<th scope="col" width=50>产品/数量/申报</th>
		<th scope="col" width=30>电池</th>
		<th scope="col" width=120> 快递单号</th>
		<th scope="col" width=40>操作</th>
	</tr>
</thead>

<tbody>	
<?php
	$commonFunction = new Common_Function();

	if(empty($result) || $result->RecordCount() < 1){
		echo "<tr><td colspan=11 align='center'>记录为空.</td></tr>";
	}else{
		while (!$result->EOF) {
			$battery_flag = $result->fields['battery_flag'];
			$order_site = strtolower($result->fields['order_site']);
			if($battery_flag  == 0 ){
				if($order_site == "hiteclaser"){
					$express_company_20 = "selected";
				}else if($order_site == "lucklaser.jp"){
					$express_company_20 = "selected";
				}else if($order_site == "kennbikyou"){
					$express_company_20 = "selected";
				}else if($order_site == "jp.civillaser"){
					$express_company_20 = "selected";
				}else{
					$express_company_20 = "";
				}
			}else{
				$express_company_20 = "";
			}
			$strTR = "<tr >";
			$strTR = $strTR."<td>".$result->fields['deliver_time']."</td>";
			if($userRole == 1){
				if( $result->fields['order_site'] == 'lucklaser.jp'){
					$strTR = $strTR."<td><a href='http://www.".$result->fields['order_site']."/img/invoice.php?oID=".$commonFunction->getOrderNumber($result->fields['order_no'],$result->fields['order_site'])."' target='_blank'>".$result->fields['order_no']."</a></td>";
				}else{
					$strTR = $strTR."<td><a href='http://www.".$result->fields['order_site'].".com/img/invoice.php?oID=".$commonFunction->getOrderNumber($result->fields['order_no'],$result->fields['order_site'])."' target='_blank'>".$result->fields['order_no']."</a></td>";
				}				
			}else{
				$strTR = $strTR."<td>".$result->fields['order_no']."</td>";
			}			
			$strTR = $strTR."<td ><textarea cols='10'  rows='2'>".$result->fields['customer_name']."</textarea><br>".$result->fields['customer_name']." </td>";		
			$strTR = $strTR."<td ><textarea cols='10'  rows='2'>".$result->fields['customer_address']."</textarea><br>".$result->fields['customer_address']." </td>";		
			$strTR = $strTR."<td ><textarea cols='5'  rows='2'>".$result->fields['customer_postcode']."</textarea><br>".$result->fields['customer_postcode']." </td>";		
			$strTR = $strTR."<td ><textarea cols='5'  rows='2'>".$result->fields['customer_telephone']."</textarea> <br>".$result->fields['customer_telephone']."</td>";		
			//$strTR = $strTR."<td ><textarea cols='5'  rows='2'>".$result->fields['customer_note']."</textarea> </td>";			
			$strTR = $strTR."<td >".$result->fields['customer_note']." </td>";			
			//$strTR = $strTR."<td ><textarea cols='2'  rows='2'>".$result->fields['product_description']."</textarea> </td>";
			$strTR = $strTR."<td >".$result->fields['product_description']." </td>";
			if($result->fields['battery_flag'] == "0"){
				$strTR = $strTR."<td><font color='green'>无</font></td>";
			}else{
				$strTR = $strTR."<td><font color='red'>有</font></td>";
			}	
			$strTR = $strTR."<td>";				
			if($result->fields['tracking_number'] == ""){				
				$strTR = $strTR."<select name='express_company_".$result->fields['id']."' id='express_company_".$result->fields['id']."'>";
				$strTR = $strTR."<option value='50' ".$express_company_50." >UPS</option>";
				$strTR = $strTR."<option value='60' ".$express_company_60." >DHL</option>";
				$strTR = $strTR."<option value='10' ".$express_company_10." >EMS</option>";
				$strTR = $strTR."<option value='20' ".$express_company_20." >日本专线Flyex</option>";
				$strTR = $strTR."<option value='30' ".$express_company_30." >TNT</option>";
				$strTR = $strTR."<option value='40' ".$express_company_40." >Fedex</option>";
				//$strTR = $strTR."<option value='50' ".$express_company_50." >UPS</option>";
				//$strTR = $strTR."<option value='60' ".$express_company_60." >DHL</option>";
				$strTR = $strTR."<option value='70' ".$express_company_70." >英法专线Rpx</option>";
				$strTR = $strTR."<option value='80' ".$express_company_80." >美国邮政Usps</option>";
				$strTR = $strTR."<option value='90' ".$express_company_90." >专线Aramex</option>";
				$strTR = $strTR."<option value='110' ".$express_company_110." >TOLL</option>";
				$strTR = $strTR."<option value='1000' ".$express_company_1000." >其他线路</option>";
				$strTR = $strTR."</select><br>";
				$strTR = $strTR."<input type='text' size=16 name='tracking_number_".$result->fields['id']."' id='tracking_number_".$result->fields['id']."'><br>";
				$strTR = $strTR."<input type='button' value='提交' onclick='addTrackingNumber(".$result->fields['id'].")'>";
			}else{	
				//显示快递记录
				$strTR = $strTR.$commonFunction->getExpressCompanyName($result->fields['express_company'])." <br> ".$result->fields['tracking_number'];
				//$strTR = $strTR."<input type='button' value='重发' onclick='addTrackingNumber2(".$result->fields['id'].")'>";		
			}			
			$strTR = $strTR."</td>";
			$strTR = $strTR."<td>";
			if($userRole == 1){//管理员
				$strTR = $strTR."<input type='button' value='删除' onclick=\"doDelete('".$result->fields['id']."','".$result->fields['order_no']."')\">";
			}else{
				//$strTR = $strTR."<input disabled='disabled' type='button' value='删除' onclick=\"doDelete('".$result->fields['id']."','".$result->fields['order_no']."')\">";				
			}	
			/*
			if($result->fields['deliver_flag'] == 2){	//2=快递单已经发给客户了
				$strTR = $strTR."<input  type='button' value='重发' onclick=\"resendPackage('".$result->fields['id']."')\">";
			}
			*/
			if($result->fields['tracking_number'] != ""){
				$strTR = $strTR."<input  type='button' value='编辑' onclick=\"editTrackingNumber('".$result->fields['id']."')\">";
			}
			$strTR = $strTR."</td>";
			$strTR = $strTR."</tr>";
			echo $strTR;
			$result->MoveNext();	
		}
	}
?>

</tbody>
					
</table>	
<br>
<!--Page 分页 -->
<div id="page"><?php echo $page->show(2);?></div>


</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" value="<?php echo $doFlag; ?>">
<input type="hidden" name="orderId" id="orderId" >
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>">
<input type="hidden" name="currentDeliverFlag" id="currentDeliverFlag" >
</form>
</body>




</html>

<script>
/************翻页跳转**************************/
function toList(listFlag){
	document.getElementById('nowPage').value = 1;//第一页
	document.getElementById('doFlag').value = "toList";
	document.getElementById('listFlag').value = listFlag;
	document.listForm.submit();
}


function goToPage(nowPage){
	var doFlag = $("#doFlag").val();
	if(doFlag == "doSearch"){
		document.getElementById('doFlag').value = 'doSearch';
	}else{
		document.getElementById('doFlag').value = 'toList';
	}	
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}

/************搜索:订单号 , 快递单号**************************/
function doSearch(){
	document.getElementById('doFlag').value = "doSearch";	
	document.listForm.submit();
}


	
function doDelete(id,orderNO){
	if(confirm("确认要删除 "+orderNO+" 的快递记录?(标记为 3=取消快递)")){
		document.getElementById('doFlag').value = "doDelete";
		document.getElementById('orderId').value = id;
		document.listForm.submit();
	}
}


function addTrackingNumber(id){
	document.getElementById('doFlag').value = "addTrackingNumber";
	document.getElementById('orderId').value = id;
	var trackingNumber = document.getElementById('tracking_number_'+id).value;
	if(trackingNumber == ""){
		alert("快递单不能为空")
		document.getElementById('tracking_number_'+id).focus();  
		return;
	}
	document.listForm.submit();
}



function editTrackingNumber(id){
	window.location.href="expressInput.php?id="+id; 
}





/************列出还没发货的订单列表*********************/
function toDeliverFlag(deliverFlag){
	document.getElementById('doFlag').value = "toDeliverFlag";
	document.getElementById('currentDeliverFlag').value = deliverFlag;	
	document.listForm.submit();
}

</script>

